<?php
/**
 * Users - пользователи системы
 * 
 * @author Mazvv
 * @package Users
 */
class Users extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'users';		
		$this->lang->load('users');
		$this->load->model('users_model');
		$this->SetNewToolSize('600x300');
		$this->SetEditToolSize('600x300');
		$this->SetDetailsToolSize('600x300');
		$this->SetFindToolSize('600x300');
		
		
		$this->a_validation_rules['_employeers_rid'] = "required";
		$this->a_validation_rules['user_login'] = "required";
		$this->a_validation_rules['edate_passwd'] = "required";
		$this->a_validation_rules['chdate_passwd'] = "required";

		$this->a_validation_fields['_employeers_rid'] = $this->lang->line('EMPLOYEER');
		$this->a_validation_fields['user_login'] = $this->lang->line('USER_LOGIN');
		$this->a_validation_fields['edate_passwd'] = $this->lang->line('END_PASSWD_DATE');
		$this->a_validation_fields['chdate_passwd'] = $this->lang->line('CHANGE_PASSWD_DATE');
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_Name){
		switch ($m_Name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}
	
	public function _journal(){
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		$content['emp_name'] =  $this->lang->line('EMPLOYEER');
		$content['user_login'] =  $this->lang->line('USER_LOGIN');
		$content['edate_passwd'] =  $this->lang->line('END_PASSWD_DATE');
		$content['chdate_passwd'] =  $this->lang->line('CHANGE_PASSWD_DATE');
		$content['filial_name'] =  $this->lang->line('FILIAL');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->users_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('users/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			
			# { _employeers_rid
			$content['_employeers_rid'] =  $this->RenderLabel('_employeers_rid');
			$employeers = array(' '=>'');
			foreach($this->users_model->GetEmployeersList() as $emp){
				$employeers[$emp->rid] = $emp->emp_name;
			}
			$content['_employeers_rid_field'] = form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:'');
			# } _employeers_rid

			# { user_login
			$content['user_login'] =  $this->lang->line('USER_LOGIN');
			$data = array('name'=>'user_login', 'value'=>$this->validation->user_login?$this->validation->user_login:'');
			$content['user_login_field'] =  form_input($data);
			# } user_login
			
			# { edate_passwd
			$content['edate_passwd'] =  $this->lang->line('END_PASSWD_DATE');
			$data = array('name'=>'edate_passwd', 'id'=>'edate_passwd', 'value'=>$this->validation->edate_passwd?$this->validation->edate_passwd:'', 'size'=>'12', 'readonly'=>'readonly');
			$content['edate_passwd_field'] =  form_input($data).$this->RenderCalendar('edate_passwd');
			# } edate_passwd
			
			# { chdate_passwd
			$content['chdate_passwd'] =  $this->lang->line('CHANGE_PASSWD_DATE');
			$data = array('name'=>'chdate_passwd', 'id'=>'chdate_passwd', 'value'=>$this->validation->chdate_passwd?$this->validation->chdate_passwd:'', 'size'=>'12', 'readonly'=>'readonly');
			$content['chdate_passwd_field'] =  form_input($data).$this->RenderCalendar('chdate_passwd');
			# } chdate_passwd
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('users/create', $content, True);
		}
		if($insRid = $this->users_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->users_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			
			# { _employeers_rid
			$content['_employeers_rid'] =  $this->RenderLabel('_employeers_rid');
			$employeers = array(' '=>'');
			foreach($this->users_model->GetEmployeersList() as $emp){
				$employeers[$emp->rid] = $emp->emp_name;
			}
			$content['_employeers_rid_field'] = form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:$content['DS']->_employeers_rid);
			# } _employeers_rid

			# { user_login
			$content['user_login'] =  $this->lang->line('USER_LOGIN');
			$data = array('name'=>'user_login', 'value'=>$this->validation->user_login?$this->validation->user_login:$content['DS']->user_login);
			$content['user_login_field'] =  form_input($data);
			# } user_login
			
			# { edate_passwd
			$content['edate_passwd'] =  $this->lang->line('END_PASSWD_DATE');
			$data = array('name'=>'edate_passwd', 'id'=>'edate_passwd', 'value'=>$this->validation->edate_passwd?$this->validation->edate_passwd:$content['DS']->edate_passwd, 'size'=>'12', 'readonly'=>'readonly');
			$content['edate_passwd_field'] =  form_input($data).$this->RenderCalendar('edate_passwd');
			# } edate_passwd
			
			# { chdate_passwd
			$content['chdate_passwd'] =  $this->lang->line('CHANGE_PASSWD_DATE');
			$data = array('name'=>'chdate_passwd', 'id'=>'chdate_passwd', 'value'=>$this->validation->chdate_passwd?$this->validation->chdate_passwd:$content['DS']->chdate_passwd, 'size'=>'12', 'readonly'=>'readonly');
			$content['chdate_passwd_field'] =  form_input($data).$this->RenderCalendar('chdate_passwd');
			# } chdate_passwd
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('users/edit', $content, True);
		}
		if($this->users_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
		    if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->users_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			
			# { _employeers_rid
			$content['_employeers_rid'] =  $this->RenderLabel('_employeers_rid');
			$employeers = array(' '=>'');
			foreach($this->users_model->GetEmployeersList() as $emp){
				$employeers[$emp->rid] = $emp->emp_name;
			}
			$content['_employeers_rid_field'] = form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:$content['DS']->_employeers_rid);
			# } _employeers_rid

			# { user_login
			$content['user_login'] =  $this->lang->line('USER_LOGIN');
			$data = array('name'=>'user_login', 'value'=>$this->validation->user_login?$this->validation->user_login:$content['DS']->user_login);
			$content['user_login_field'] =  form_input($data);
			# } user_login
			
			# { edate_passwd
			$content['edate_passwd'] =  $this->lang->line('END_PASSWD_DATE');
			$data = array('name'=>'edate_passwd', 'id'=>'edate_passwd', 'value'=>$this->validation->edate_passwd?$this->validation->edate_passwd:$content['DS']->edate_passwd, 'size'=>'12', 'readonly'=>'readonly');
			$content['edate_passwd_field'] =  form_input($data);
			# } edate_passwd
			
			# { chdate_passwd
			$content['chdate_passwd'] =  $this->lang->line('CHANGE_PASSWD_DATE');
			$data = array('name'=>'chdate_passwd', 'id'=>'chdate_passwd', 'value'=>$this->validation->chdate_passwd?$this->validation->chdate_passwd:$content['DS']->chdate_passwd, 'size'=>'12', 'readonly'=>'readonly');
			$content['chdate_passwd_field'] =  form_input($data);
			# } chdate_passwd
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('users/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->users_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND'); 
			# {  emp_name
			$content['emp_name'] =  $this->lang->line('EMPLOYEER');
			$content['emp_name_field'] = form_input('emp_name');
			# }  emp_name
			
			# {  filial_name
			$content['filial_name'] =  $this->lang->line('FILIAL');
			$content['filial_name_field'] =  form_input('filial_name');
			# {  filial_name
			
			# {  user_login
			$content['user_login'] =  $this->lang->line('USER_LOGIN');
			$content['user_login_field'] = form_input('user_login');
			# }  user_login
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('  '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			
			return $this->load->view('users/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['emp_name']) && trim($_POST['emp_name'])) $searchRule['CONCAT(_employeers.f_name, \' \', _employeers.s_name, \' \', _employeers.l_name)'] = trim($_POST['emp_name']);
		if(isset($_POST['filial_name']) && trim($_POST['filial_name'])) $searchRule['_filials.name'] = trim($_POST['filial_name']);
		if(isset($_POST['user_login']) && trim($_POST['user_login'])) $searchRule['_users.user_login'] = trim($_POST['user_login']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_users.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_users.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('users', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
	
}

?>